1 00:00:00,750 --> 00:00:07,020 In this lecture, I want to talk about the powerful logical operators we have in Lua that allow us to 2 00:00:07,020 --> 00:00:09,900 make our conditions more complex. 3 00:00:09,930 --> 00:00:15,600 These logical operators are and, or and not. 4 00:00:15,810 --> 00:00:20,820 Using these operators, we can make condition checking more complex, as we can watch for more than 5 00:00:20,820 --> 00:00:24,030 one condition to be true or false. 6 00:00:24,240 --> 00:00:28,530 So as an example, what I'm going to do is I'm going to make several variables here. 7 00:00:29,100 --> 00:00:34,560 I'm going to call this first variable example num one. 8 00:00:34,560 --> 00:00:37,030 And we can set it equal to a value of ten. 9 00:00:37,050 --> 00:00:39,060 And then I'm going to create a second variable. 10 00:00:39,060 --> 00:00:42,840 We can call this example num two and set it equal to 50. 11 00:00:43,440 --> 00:00:48,750 And then I'm going to create another variable I'll just call this example bool. 12 00:00:48,750 --> 00:00:51,090 And I'm going to set it equal to true. 13 00:00:51,670 --> 00:00:56,350 Then what I'm going to do is I'm going to use an if statement and we're going to check some conditions 14 00:00:56,350 --> 00:00:56,700 here. 15 00:00:56,710 --> 00:01:03,070 So for example, maybe I want to check if example num one is equal to ten right. 16 00:01:03,750 --> 00:01:08,660 If example number one is equal to ten, then I want to execute this block of code here. 17 00:01:08,670 --> 00:01:14,040 But maybe I also want to make sure that example number two is also equal to 50. 18 00:01:14,070 --> 00:01:16,920 This is where we can use the and keyword. 19 00:01:16,920 --> 00:01:21,240 So I'm going to type out and here and then we can pass another condition to check. 20 00:01:21,240 --> 00:01:26,040 So I want to check if example number two is equal to 50. 21 00:01:26,070 --> 00:01:28,800 This is reading out like basic English. 22 00:01:28,830 --> 00:01:34,740 If the value stored in this variable is equal to ten and the value stored in this variable is equal 23 00:01:34,740 --> 00:01:38,430 to 50, then we're going to execute this block of code. 24 00:01:38,550 --> 00:01:45,510 This means that both of these conditions must evaluate to true in order for this block of code to execute. 25 00:01:45,540 --> 00:01:50,880 If either one of these conditions evaluates to false, then it's not going to execute. 26 00:01:50,910 --> 00:01:56,130 So when the interpreter goes and it checks the first condition, if this first condition evaluates to 27 00:01:56,130 --> 00:02:00,420 false, it's going to ignore the second condition and just skip this if statement block. 28 00:02:00,690 --> 00:02:05,190 If the first condition is true, then it's going to check the second condition. 29 00:02:05,190 --> 00:02:09,420 If the second condition is false, then it's going to ignore this block of code. 30 00:02:09,420 --> 00:02:13,680 However, if both are true, then we're going to execute what's inside of here. 31 00:02:13,710 --> 00:02:15,120 So I can print. 32 00:02:15,480 --> 00:02:18,720 The conditions were truthy. 33 00:02:19,680 --> 00:02:22,410 So now if I go ahead and run my game. 34 00:02:24,080 --> 00:02:29,090 As you can see, the conditions were truthy because the value stored in the first variable was ten and 35 00:02:29,090 --> 00:02:31,580 the value stored in the second variable was 50. 36 00:02:31,670 --> 00:02:34,180 Let's go ahead and make this a little bit more complex. 37 00:02:34,190 --> 00:02:39,290 What I want to do is now use the Or keyword, and I want to check a third condition here. 38 00:02:39,290 --> 00:02:42,680 I'm going to pass our example bool here. 39 00:02:43,310 --> 00:02:46,100 And I want to check to see if this is true. 40 00:02:46,100 --> 00:02:49,400 So we can type out if the value in here is equal to true. 41 00:02:49,400 --> 00:02:51,440 Or we can completely omit that. 42 00:02:51,440 --> 00:02:56,720 And it will just check to see if a value exists within our variable or the value inside of there is 43 00:02:56,720 --> 00:02:57,410 truthy. 44 00:02:57,770 --> 00:03:03,560 Now what I'm going to do next is I'm going to change the value inside of example number to equal to 45 00:03:03,590 --> 00:03:04,400 40. 46 00:03:04,850 --> 00:03:06,740 What do you think is going to happen here? 47 00:03:06,770 --> 00:03:10,730 Do you think this block of code is going to execute or not execute? 48 00:03:11,030 --> 00:03:12,230 Well let's see what happens. 49 00:03:12,230 --> 00:03:13,340 We can hit run. 50 00:03:14,990 --> 00:03:17,810 And as you can see, it says the conditions were truthy. 51 00:03:17,840 --> 00:03:18,620 Wait, what? 52 00:03:18,650 --> 00:03:19,700 How were the conditions? 53 00:03:19,700 --> 00:03:20,300 Truthy. 54 00:03:20,330 --> 00:03:22,850 Let's go ahead and take another look again. 55 00:03:23,240 --> 00:03:28,910 So when our interpreter has reached this if statement, what it's going to do is it's going to check 56 00:03:28,910 --> 00:03:30,140 this first condition. 57 00:03:30,410 --> 00:03:33,380 Of course this first condition evaluated to true. 58 00:03:33,650 --> 00:03:36,110 And then it's going to check the second condition. 59 00:03:36,940 --> 00:03:40,390 Well, the second condition is false, right? 60 00:03:40,390 --> 00:03:43,120 So it should have ignored this block of code, right? 61 00:03:43,240 --> 00:03:47,410 Actually it's not going to because we have the Or statement here. 62 00:03:47,440 --> 00:03:53,830 So basically what's happening here is that either example number one and example number two have to 63 00:03:53,830 --> 00:03:55,840 be equal to their respective values. 64 00:03:55,840 --> 00:04:02,680 Or just this single variable right here has to contain a value or evaluate to be true. 65 00:04:03,070 --> 00:04:06,160 Either these two values have to be true right. 66 00:04:06,190 --> 00:04:09,610 Or just this value right here has to be true. 67 00:04:09,910 --> 00:04:13,600 Now maybe you don't want it to be evaluated in this way. 68 00:04:13,600 --> 00:04:20,290 And this is where we can use parentheses in order to change how this if statement is evaluated. 69 00:04:20,290 --> 00:04:25,030 For example, what I'm going to do here is I'm going to wrap this first condition in parentheses. 70 00:04:26,110 --> 00:04:31,120 And then I'm going to wrap these other two conditions and another pair of parentheses. 71 00:04:32,240 --> 00:04:37,620 Now we have changed how this condition is going to be evaluated. 72 00:04:37,670 --> 00:04:40,640 So what's going to happen here is it's going to check this first condition. 73 00:04:40,730 --> 00:04:47,150 If this first condition evaluates to true then that means what's going to happen next is either example 74 00:04:47,150 --> 00:04:49,220 number two has to be equal to 50. 75 00:04:49,220 --> 00:04:52,190 Or example bool has to be truthy. 76 00:04:52,220 --> 00:04:54,260 Because we have inserted these parentheses. 77 00:04:54,260 --> 00:05:00,320 Here we've changed the logic of how this if statement or these conditions are going to be evaluated. 78 00:05:00,680 --> 00:05:02,480 So this has to be true. 79 00:05:02,480 --> 00:05:06,500 And either this has to be true or this has to be true. 80 00:05:07,030 --> 00:05:11,950 So that means if we go ahead and run the game, we're still going to have it printed out into the console 81 00:05:11,980 --> 00:05:14,740 because that last condition evaluated to true. 82 00:05:16,200 --> 00:05:21,900 But if we set the value inside of there equal to false, and then we go and run our game. 83 00:05:23,990 --> 00:05:29,720 As you can see, nothing was printed inside of the console because even though this condition here evaluated 84 00:05:29,720 --> 00:05:36,800 to true, neither this condition nor this condition evaluated to true, so it did not execute this if 85 00:05:36,800 --> 00:05:37,760 statement block. 86 00:05:38,890 --> 00:05:43,870 The last operator we're going to take a look at is the not operator, which is an interesting one. 87 00:05:43,870 --> 00:05:45,100 Why is it interesting? 88 00:05:45,100 --> 00:05:51,270 Because it can negate truthy values and make falsy values true. 89 00:05:51,280 --> 00:05:57,580 So for example, if I had a falsy value and I placed the Not keyword before it, it would now become 90 00:05:57,610 --> 00:06:00,430 truthy because it negated the falsy value. 91 00:06:00,460 --> 00:06:06,100 You can think of it like multiplying a negative number with a negative number to make a positive number, 92 00:06:06,100 --> 00:06:11,770 and this is also the case if you multiply a negative number with a positive number to make that positive 93 00:06:11,770 --> 00:06:12,760 number negative. 94 00:06:12,760 --> 00:06:19,630 So if you put the not keyword behind a truth value, it would become false or evaluate to false. 95 00:06:19,630 --> 00:06:22,570 So as an example here I'm going to create another if statement. 96 00:06:22,570 --> 00:06:24,760 And we're going to use the not keyword. 97 00:06:25,210 --> 00:06:31,660 And what we're going to do is we're going to negate the value stored in our example boolean variable. 98 00:06:31,660 --> 00:06:41,620 So if not example bool then what we're going to do is print out the condition was false but the not 99 00:06:41,710 --> 00:06:43,690 made it truthy. 100 00:06:44,440 --> 00:06:48,460 So basically the value inside of this variable was false. 101 00:06:48,460 --> 00:06:54,790 But because of the not keyword here, we forced this to evaluate to true and execute this block of code. 102 00:06:54,820 --> 00:06:57,580 You can again think of this as basic English. 103 00:06:57,640 --> 00:07:03,540 If this is not true, then we're going to execute this block of code. 104 00:07:03,550 --> 00:07:09,100 And since the example boolean variable stores the value of false which is not true, then this is going 105 00:07:09,100 --> 00:07:10,000 to execute. 106 00:07:10,030 --> 00:07:15,520 So if we go ahead and hit run here as you can see it prints out the condition was false. 107 00:07:15,520 --> 00:07:21,650 But the not made it truthy to continue demonstrating the power of this not operator. 108 00:07:21,670 --> 00:07:28,090 What I'm going to do is I'm going to put a print statement here and I'm going to print out not not nil. 109 00:07:28,480 --> 00:07:31,600 What do you think is going to print out inside of the console. 110 00:07:31,630 --> 00:07:34,570 Well, let's go ahead and hit run and see what happens. 111 00:07:36,130 --> 00:07:38,800 As you can see, it printed out false. 112 00:07:38,800 --> 00:07:40,840 And you'll see why here in a moment. 113 00:07:40,870 --> 00:07:46,780 So what's happening here is not nil is true, right? 114 00:07:46,810 --> 00:07:50,410 We've negated the value of nil and made it evaluate to true. 115 00:07:50,440 --> 00:07:52,570 However, we have a second. 116 00:07:52,600 --> 00:07:53,620 Not here. 117 00:07:53,710 --> 00:07:59,950 And since not nil is true, and then not true is, you guessed it, false. 118 00:07:59,950 --> 00:08:02,830 So we print it out false inside of the console. 119 00:08:02,860 --> 00:08:10,690 Another use case we have for these logical operators are assigning default values to variables. 120 00:08:10,780 --> 00:08:12,130 I'm going to show you what I mean. 121 00:08:12,800 --> 00:08:14,000 I'm going to create a variable. 122 00:08:14,000 --> 00:08:18,440 I'm going to call it this Val, and I'm going to set it equal to nil. 123 00:08:18,440 --> 00:08:20,300 So it has no value inside of it. 124 00:08:20,540 --> 00:08:22,490 Then I'm going to create another variable. 125 00:08:22,490 --> 00:08:25,130 I'm going to call this some var. 126 00:08:25,670 --> 00:08:28,970 And what I'm going to do is I'm going to set it equal to this Val. 127 00:08:29,000 --> 00:08:34,550 But then I'm going to use the Or keyword here and put the value of like ten. 128 00:08:34,640 --> 00:08:41,120 So what's going to happen here is that the value stored inside of some bar is going to be this val. 129 00:08:41,150 --> 00:08:48,560 Or if this val happens to be false or it evaluates to false, then the value of ten is going to be stored 130 00:08:48,560 --> 00:08:49,550 in some var. 131 00:08:49,760 --> 00:08:52,460 And this is because of how conditions work. 132 00:08:52,460 --> 00:08:54,350 Conditions are going to return. 133 00:08:54,350 --> 00:09:01,940 The first value that is truthy, or a condition is going to return the last value when all previous 134 00:09:01,940 --> 00:09:07,850 conditions were false, regardless if the last value is truthy or not. 135 00:09:12,300 --> 00:09:18,000 So as an example, we can demonstrate the power of these logical operators in real time by using the 136 00:09:18,000 --> 00:09:18,850 command line. 137 00:09:18,870 --> 00:09:23,880 If you do not have the command line open, you can head up to the view tab and open the command line 138 00:09:23,880 --> 00:09:26,010 or the command bar right here. 139 00:09:26,600 --> 00:09:30,980 And what I'm going to do inside of the command line is I'm going to print out into the console. 140 00:09:30,980 --> 00:09:34,100 We're going to print out nil or ten. 141 00:09:34,460 --> 00:09:40,070 And when we hit enter, as you can see, what happens is we got ten printed out into the console. 142 00:09:40,070 --> 00:09:44,240 And as I said earlier, this is because conditions will return. 143 00:09:44,240 --> 00:09:52,940 The first value that it finds is truthy, or it's going to return the last value regardless if it is 144 00:09:52,940 --> 00:09:53,860 truthy or not. 145 00:09:53,870 --> 00:10:01,310 And to demonstrate that what I'm going to do is we could pass nil here or we could pass false. 146 00:10:01,310 --> 00:10:01,790 Right? 147 00:10:01,820 --> 00:10:05,390 What I think is going to print nil or false if we hit enter. 148 00:10:05,540 --> 00:10:09,560 As you can see, we got false because nil evaluated to true. 149 00:10:09,560 --> 00:10:14,240 And since false is the last condition being checked here, it got returned by default. 150 00:10:14,240 --> 00:10:16,660 So that's why false printed inside of the console. 151 00:10:16,670 --> 00:10:23,960 If we flip the position of these two values and I put false first or nil, and then we hit enter. 152 00:10:24,050 --> 00:10:28,430 As you can see nil printed instead because this first condition evaluated to false. 153 00:10:28,430 --> 00:10:32,930 And since nil is the last condition being checked, it got returned by default. 154 00:10:32,930 --> 00:10:39,920 And using this behavior, we can create default values for variables in case one of them happens to 155 00:10:39,920 --> 00:10:40,550 be false. 156 00:10:40,550 --> 00:10:48,440 So for example, in any case, you may want a player to define or update their own client sided settings. 157 00:10:48,440 --> 00:10:52,280 So for example you might have a default setting for some particular thing. 158 00:10:52,280 --> 00:10:55,330 And the client can change that setting to their preference. 159 00:10:55,340 --> 00:11:01,430 What we could do is we could check to see if a player has changed a setting to their own personal preference. 160 00:11:01,460 --> 00:11:08,330 If not, we can assign a default value that we've created for all the players to have when they don't 161 00:11:08,360 --> 00:11:10,580 manipulate any settings on their end. 162 00:11:10,580 --> 00:11:16,220 So that means when I go and print some var into the console, we're going to get the value of ten here. 163 00:11:16,220 --> 00:11:18,470 And that's because this value is nil. 164 00:11:18,500 --> 00:11:19,820 It evaluates to false. 165 00:11:19,820 --> 00:11:22,910 So it defaults with the value of ten. 166 00:11:22,910 --> 00:11:24,800 So if we go and run our game here. 167 00:11:25,590 --> 00:11:28,950 As you can see, we got ten printed out inside of our console. 168 00:11:29,540 --> 00:11:31,880 Now if I put a different value in this val. 169 00:11:31,880 --> 00:11:38,510 For example, if I put a string like hello, since the string is true, that means this value is going 170 00:11:38,510 --> 00:11:43,970 to be stored in some var and it's going to ignore ten because like I said, the first value that evaluates 171 00:11:43,970 --> 00:11:49,800 to true is going to be stored or returned from this condition and stored inside of our variable. 172 00:11:49,820 --> 00:11:55,280 So if I hit run, as you can see, we get hello printed into the console. 173 00:11:56,210 --> 00:12:02,150 Logical operators have so many uses in Lua and you can use them in many different places. 174 00:12:02,150 --> 00:12:07,860 So please, I'd really recommend for you to continue messing with logical operators in your scripts. 175 00:12:07,880 --> 00:12:13,130 Take a few minutes using the command line down here to get a good understanding of how these logical 176 00:12:13,160 --> 00:12:14,420 operators work. 177 00:12:14,510 --> 00:12:17,330 After you do that, I will see you in the next lecture.